﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TripsLogic.controller;
using System.Data;

namespace TripsLogic
{
    public partial class tripdetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                loadLang();
                loadDetail();
            }
            ClientScript.RegisterStartupScript(Page.GetType(), "OnLoad", "calcRoute();", true);
            
        }
        private void loadLang()
        {
            litPriceTitle.Text = LangController.getLng("litPriceTitle.Text", "Price");
            litTimeTitle.Text = LangController.getLng("litTimeTitle.Text", "Date");
            litCarTitle.Text = LangController.getLng("litCarTile.Text", "Car type");
            litPreferTitle.Text = LangController.getLng("litPreferTitle.Text", "Preferences");
            litCommentTitle.Text = LangController.getLng("litCommentTitle.Text", "Comment");
            litFromTitle.Text = LangController.getLng("litFromTitle.Text", "FROM");
            litToTitle.Text = LangController.getLng("litToTitle.Text", "TO");
            litDriverText.Text = LangController.getLng("litDriverText.Text", "Driver");
            listPassengerText.Text = LangController.getLng("listPassengerText.Text", "Passenger");
            litAvaibilityText.Text = LangController.getLng("litAvaibilityText.Text", "Avaibility");
            litGoogleMap.Text = LangController.getLng("litGoogleMap.Text", "Google Map");
            litCurrency.Text = LangController.getLng("litCurrency.Text", "DKK");
            litMapDes.Text = LangController.getLng("litMapDesText", " Distance = ");
            
        }
       
        private void loadDetail()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
            {
                string id = Request.QueryString["id"].ToString();
                id = id.Substring(id.LastIndexOf('-') + 1, id.Length - id.LastIndexOf('-') - 1);
                WeSTrip.WeSTripClient client = new WeSTrip.WeSTripClient();
                DataTable dt = TripController.getTripById(Convert.ToInt32(id));
                
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataTable dtUser = SysUserController.getUserById(dt.Rows[0]["SysUserId"].ToString());
                    DataTable dtCar = TripController.getCarbyTripId(Convert.ToInt32(dt.Rows[0]["CarId"]));

                    DataTable dtTripPref = TripController.getTripPreferenceById(Convert.ToInt32(id));

                    string avatarImagePath = SysIniController.getSetting("Avatar_ImagePath", "images/avatar/");

                    string tag = "";
                    for (int i = 0; i < dtTripPref.Rows.Count; i++)
                    {
                        DataTable prefInfor = TripController.getPreferenceById(Convert.ToInt32(dtTripPref.Rows[i]["PreferenceId"]));
                        tag = tag + "<i style='color:#92dc8b; margin-right:5px;' class='fa " + prefInfor.Rows[0]["Image"] + "  fa-lg'></i>";
                    }
                    string[] arrF = dt.Rows[0]["AddressFrom"].ToString().Split(',');
                    string from = "";
                    for (int i = 0; i < arrF.Length - 1; i++)
                    {
                        from = from + arrF[i];
                    }
                    string[] arrT = dt.Rows[0]["AddressTo"].ToString().Split(',');
                    string to = "";
                    for (int i = 0; i < arrT.Length - 1; i++)
                    {
                        to = to + arrT[i];
                    }

                    
                    start.Text = dt.Rows[0]["AddressFrom"].ToString();
                    destination.Text = dt.Rows[0]["AddressTo"].ToString();

                    int currentSlot = TripController.getCurrentSlot(Convert.ToInt32(dt.Rows[0]["Id"]));
                    int remainSlot = Convert.ToInt32(dt.Rows[0]["AvaiableSlot"].ToString()) - currentSlot;

                    litFrom.Text = " <span class='address'>"+ from +" </span> ";
                    litTo.Text = " <span class='address'>" + to+ " </span>";
                    litPriceNo.Text =""+ dt.Rows[0]["Price"];
                    litDriverName.Text = "" + dtUser.Rows[0]["LName"].ToString() + " " + dtUser.Rows[0]["FName"].ToString();
                    litCar.Text = "" + dtCar.Rows[0]["CarModel"];
                    litComment.Text = ""+dt.Rows[0]["Comment"];
                    litPrefer.Text = tag;
                    litNumAvai.Text = "&nbsp;&nbsp;" + remainSlot.ToString() + "&nbsp;&nbsp;";
                    string avatar= avatarImagePath + dtUser.Rows[0]["Avatar"].ToString();
                    litImagDriver.Text = "<img src='"+avatar+"' height='127px' width='127px' />";
                    DataTable dtJoin = TripController.getJoinbyTripId(Convert.ToInt32(id));
                    string guest = "";
                    string empty = "";
                    if (dtJoin.Rows.Count != 0)
                    {
                        for (int i = 0; i < dtJoin.Rows.Count; i++)
                        {
                            if (dtJoin.Rows[i]["IsApproved"].ToString() == "True" && Convert.ToInt32(dtJoin.Rows[i]["StatusId"].ToString()) == 1)
                            {
                                DataTable dtUser2 = SysUserController.getUserById(dtJoin.Rows[i]["SysUserId"].ToString());
                                avatar = avatarImagePath + dtUser2.Rows[0]["Avatar"].ToString();
                                string guestName="" + dtUser2.Rows[0]["LName"].ToString() + " " + dtUser2.Rows[0]["FName"].ToString();
                                guest = guest+"<div style='float:left; margin-left:10px'><img src='"+avatar+"' height='90px' width='90px' />" +
                                   "<div  style=' margin:5px 5px 5px 5px; font-size: 14px;' align='center'>"+guestName+"</div></div>";
                            }
                        }
                    }
                    for (int i = 0; i < remainSlot; i++)
                    {
                        string avatarDefault = SysIniController.getSetting("Avatar_Default", "avatardefault.png");
                        avatar = avatarImagePath + avatarDefault;
                        string noName=  LangController.getLng("noName", "None");
                        empty = empty+"<div style='float:left; margin-left:10px'><img src='"+avatar+"' height='90px' width='90px' />" +
                                   "<div  style=' margin:5px 5px 5px 5px; font-size: 14px;' align='center'>" + noName + "</div></div>";
                    }
                    litguestinfo.Text = empty + guest;
                    litJointButton.Text = "<a href='#' class='buttoncss'>Join Trip</a>";

                }
            }
        }

    }
}